// 获取当前应用实例
const appInstance = getApp()

class uniRequest {
	defaults = {
		baseUrl: '',
		url: '',
		data: null,
		method: "GET",
		header: {
			"Content-type": "application/json"
		},
		timeOut: 6000, //小程序默认超时时长是一分钟
		isLoading: true
	}
	// 拦截器
	interceptors = {
		request: (config) => config,
		response: (response) => response
	}
	//定义数组队列
	queue = []
	// 用户创建和初始化类的属性以及方法
	//在实例化时传入的参数，会被constructor形参进行接收
	constructor(params = {}) {
		this.defaults = Object.assign({}, this.defaults, params)
	}
	request(options) {
		//如果有新的请求，就清除上一次的定时器
		this.timerID && clearTimeout(this.timerID)
		//合并完整的请求地址
		options.url = this.defaults.baseUrl + options.url
		//合并完整的请求地址
		options = {
			...this.defaults,
			...options
		}

		//在请求添加发送之前添加loading效果
		if (options.isLoading && options.method !== 'UPLOAD') {
			//判断队列是否为空，如果是空，就显示loading,如果不是空，就不显示loading,不调用uni.loading
			if (this.queue.length === 0) {
				uni.showLoading({
					title: '加载中...'
				})
				//然后立即向queue数组队列中增加请求标识，每个标识代表一个请求，标识是自定义的
				this.queue.push('request')
			}


		}

		//在发送之前，使用请求拦截器，新增和修改请求参数
		options = this.interceptors.request(options)

		//需要使用promise封装uni.request请求
		return new Promise((resolve, reject) => {
			if (options.method === 'UPLOAD') {
				uni.uploadFile({
					...options,
					success: (res) => {
						//需要将服务器返回的Json字符串转成对象
						res.data = JSON.parse(res.data)
						//合并参数
						const mergeRes = Object.assign({}, res, {
							config: options,
							isSuccess: true
						})
						resolve(this.interceptors.response(mergeRes))
					},
					fail: (err) => {
						//合并参数
						const mergeErr = Object.assign({}, err, {
							config: options,
							isSuccess: false
						})
					}
				})
			} else {
				uni.request({
					...options,
					success: (res) => {
						const mergeRes = Object.assign({}, res, {
							config: options,
							isSuccess: true
						})
						resolve(this.interceptors.response(mergeRes))
					},
					fail: (err) => {
						const mergeRes = Object.assign({}, res, {
							config: options,
							isSuccess: true
						})
						reject(this.interceptors.response(mergeRes))
					},
					complete: () => {
						if (options.isLoading) {
							//在每一个请求结束之后都会执行complete回调函数
							//每次从queue队列中删除一个标识
							this.queue.pop()
							this.queue.length === 0 && this.queue.push('request')

							this.timerID = setTimeout(() => {
								this.queue.pop()
								this.queue.length === 0 && uni.hideLoading()
							}, 1)
						}
					}
				})
			}
		})

	}
	// 快捷方法封装
	get(url, data = {}, config = {}) {
		return this.request(Object.assign({
			url,
			data,
			method: 'GET'
		}, config))
	}
	post(url, data = {}, config = {}) {
		return this.request(Object.assign({
			url,
			data,
			method: 'POST'
		}, config))
	}
	put(url, data = {}, config = {}) {
		return this.request(Object.assign({
			url,
			data,
			method: 'PUT'
		}, config))
	}
	delete(url, data = {}, config = {}) {
		return this.request(Object.assign({
			url,
			data,
			method: 'DELETE'
		}, config))
	}
	// 并发
	all(...promise) {
		return Promise.all(promise)
	}
	upload(url, filePath, name = 'file', config = {}) {
		return this.request(Object.assign({
			url,
			data,
			method: 'DELETE'
		}, config))
	}
}

export default uniRequest